<?php
/*
Template Name: Events (Diary)
Auther: Naseer Ahnad
Created: 27-03-2012
*/
?>
<?php
function getDayName($id){
	switch($id){
		case 0:return "Sunday";
		case 1:return "Monday";
		case 2:return "Tuesday";
		case 3:return "Wednesday";
		case 4:return "Thursday";
		case 5:return "Friday";
		case 6:return "Saturday";
	}
}

function getEventIDs($userID, $date){
	global $wpdb;
	$qCount=2;
	$query="SELECT post_id FROM wp_postmeta WHERE (meta_key = 'eventDate' AND meta_value = '".$date."') 
		OR (meta_key='userID' AND meta_value='".$userID."')";
	$query.="  GROUP BY post_id HAVING count(*) = ".$qCount;
	$result=$wpdb->get_results($query, ARRAY_A);
	return $result;
}

function isOldDate($d1, $d2) {
// Return the number of days between the two dates:
	$val=strtotime($d2)-strtotime($d1);
	if($val<0) return true; else return false;
}  // end function dateDiff

wp_get_current_user();
if ( $current_user->ID == 0 ) {	// Not logged in.
 header("Location: http://armchaircorporateguru.co.uk/login/");
 exit;
}
    
$userID = $current_user->ID;

if(isset($_GET['show'])) $show=$_GET['show']; else $show=date("Y-m");
$dateToShow=date("Y-m-d",strtotime($show."-1"));
$month = date("m", strtotime($dateToShow));
$year = date("Y", strtotime($dateToShow));
$wdNo=date("w",strtotime($dateToShow));
$totalDays = cal_days_in_month(CAL_GREGORIAN, $month, $year);

$prevMonth= date("Y-m",strtotime($dateToShow." -1 month"));
$nextMonth= date("Y-m",strtotime($dateToShow." +1 month"));

?>
<script type="text/javascript">
function popupAdd(d){
	show='<?=$show?>';
	url="http://armchairweddingguru.co.uk/event-handler/?date="+d;
	window.showModalDialog(url,null,"dialogWidth:400px; dialogHeight:200px; center:yes");
	window.location="http://armchairweddingguru.co.uk/diary/?show="+show;
}

function popupEdit(id){
	show='<?=$show?>';
	url="http://armchairweddingguru.co.uk/event-handler/?id="+id;
	window.showModalDialog(url,null,"dialogWidth:400px; dialogHeight:200px; center:yes");
	window.location="http://armchairweddingguru.co.uk/diary/?show="+show;
}

</script>


<?php get_header(); ?>
    <div class="main-content-inner global-width">
    
        	<h1>Your Events </h1>
            <p>Here are your list of up coming events, with the next event being the closest to the top. Checking this regular will ensure you do not miss a planned event.</p>
            
            <div class="events full-width">
                <h2>
                	<a href="http://armchairweddingguru.co.uk/diary/?show=<?=$prevMonth?>">
                    	<img src="<?php bloginfo( 'template_url' );?>/images/arrow-left.gif" alt="Image" class="float-left" />
                    </a>
                    <?php echo date("F Y",strtotime($dateToShow))?>
                    <a href="http://armchairweddingguru.co.uk/diary/?show=<?=$nextMonth?>">
                    	<img src="<?php bloginfo( 'template_url' );?>/images/arrow-right.gif" alt="Image" class="float-right" />
                    </a>
                    <div class="clearfix"></div>
                </h2>
                
                <ul>
					<?php for($i=0;$i<=6;$i++): ?>
                    <li><?=getDayName($wdNo)?></li>
                    <?php 
                        $wdNo++;
                        if($wdNo>6) $wdNo=0;
                        endfor; 
                    ?>
                </ul>
                
                <ul class="calender">
				<?php
                for($i=1; $i<=$totalDays; $i++):
                $thisDate=date("Y-m-d",strtotime($show."-$i"));
                $newEvent= "popupAdd('$thisDate')";
				$eventIDs = getEventIDs($userID, $thisDate);
                ?>
                	<li>
                    	<?php if(isOldDate(date("Y-m-d"),$thisDate)==false): ?>
                    	<a href="javascript:void()" onclick="<?=$newEvent?>">
                        	<img src="<?php bloginfo( 'template_url' );?>/images/add.png" width="24" alt="Image" class="float-left" /> Add Event
                        </a>
                        <?php endif; ?>
                        <span><?php echo $i ?></span>
                        <?php 
							foreach($eventIDs as $eventID):
							$eventInfo = get_post($eventID['post_id'], ARRAY_A);
							$editEvent= "popupEdit(".$eventInfo['ID'].")";
						 ?>
                        <div style="display:block; float:none; clear:both">
                        	<a href="javascript:void()" onclick="<?=$editEvent?>"><?php echo $eventInfo['post_title']; ?></a>
                         </div>
                        <?php endforeach; ?>
                    </li>
                <?php endfor; ?>
                </ul>

            </div>
             	      	     
    	<div class="clearfix"></div>
    </div>
 
<!-- Main Content Ends -->
</section>

<?php get_footer(); ?>